<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const loading = ref(false)
const errorMessage = ref('')
const successMessage = ref('')

const formData = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validateForm = () => {
  if (!formData.value.oldPassword) {
    errorMessage.value = '请输入原密码'
    return false
  }
  if (!formData.value.newPassword) {
    errorMessage.value = '请输入新密码'
    return false
  }
  if (formData.value.newPassword.length < 6) {
    errorMessage.value = '新密码长度不能少于6位'
    return false
  }
  if (formData.value.newPassword !== formData.value.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return false
  }
  return true
}

const handleSubmit = async () => {
  if (!validateForm()) return

  try {
    loading.value = true
    errorMessage.value = ''
    successMessage.value = ''

    const response = await fetch('http://localhost:3001/api/admin/change-password', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      },
      body: JSON.stringify({
        oldPassword: formData.value.oldPassword,
        newPassword: formData.value.newPassword
      })
    })

    const data = await response.json()

    if (response.ok) {
      successMessage.value = '密码修改成功'
      // 清空表单
      formData.value = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
      // 3秒后返回上一页
      setTimeout(() => {
        router.back()
      }, 3000)
    } else {
      errorMessage.value = data.message || '密码修改失败'
    }
  } catch (error) {
    console.error('修改密码失败:', error)
    errorMessage.value = '服务器错误，请稍后重试'
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="change-password">
    <div class="form-card">
      <div class="card-header">
        <h2>修改密码</h2>
        <button class="back-btn" @click="router.back()">
          <i class="fa-solid fa-arrow-left"></i>
          返回
        </button>
      </div>

      <form @submit.prevent="handleSubmit" class="password-form">
        <div class="form-group">
          <label>原密码</label>
          <div class="input-wrapper">
            <input
              v-model="formData.oldPassword"
              type="password"
              placeholder="请输入原密码"
              required
            >
          </div>
        </div>

        <div class="form-group">
          <label>新密码</label>
          <div class="input-wrapper">
            <input
              v-model="formData.newPassword"
              type="password"
              placeholder="请输入新密码"
              required
            >
          </div>
          <div class="form-tip">密码长度不少于6位</div>
        </div>

        <div class="form-group">
          <label>确认新密码</label>
          <div class="input-wrapper">
            <input
              v-model="formData.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              required
            >
          </div>
        </div>

        <div v-if="errorMessage" class="error-message">
          <i class="fa-solid fa-circle-exclamation"></i>
          {{ errorMessage }}
        </div>

        <div v-if="successMessage" class="success-message">
          <i class="fa-solid fa-circle-check"></i>
          {{ successMessage }}
        </div>

        <div class="form-actions">
          <button type="submit" :disabled="loading" class="submit-btn">
            {{ loading ? '提交中...' : '确认修改' }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>
.change-password {
  padding: 24px;
  max-width: 600px;
  margin: 0 auto;
}

.form-card {
  background: var(--bg-color);
  border-radius: var(--border-radius);
  box-shadow: var(--box-shadow);
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
  color: var(--text-color);
}

.back-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  transition: all 0.3s;
}

.back-btn:hover {
  color: var(--primary-color);
  border-color: var(--primary-color);
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

label {
  font-weight: 500;
  color: var(--text-color);
}

.input-wrapper {
  position: relative;
}

input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  background: var(--bg-color);
  color: var(--text-color);
  font-size: 14px;
}

input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.2);
}

.form-tip {
  font-size: 12px;
  color: var(--text-color-secondary);
}

.error-message {
  padding: 8px 12px;
  background: #fff2f0;
  border: 1px solid #ffccc7;
  border-radius: var(--border-radius);
  color: #ff4d4f;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.success-message {
  padding: 8px 12px;
  background: #f6ffed;
  border: 1px solid #b7eb8f;
  border-radius: var(--border-radius);
  color: #52c41a;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 24px;
}

.submit-btn {
  padding: 8px 24px;
  background: var(--primary-color);
  color: white;
  border: none;
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: all 0.3s;
}

.submit-btn:hover {
  opacity: 0.8;
}

.submit-btn:disabled {
  background: var(--border-color);
  cursor: not-allowed;
  opacity: 1;
}
</style> 